package org.sky.axis2.soap;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPFault;
import org.apache.axiom.soap.SOAPFaultCode;
import org.apache.axiom.soap.SOAPFaultReason;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;

public class SoapFaultService {
	private int i = 0;

	public OMElement getPrice(OMElement request) throws AxisFault {
		if (request == null) {
			SOAPFault fault = getSOAPFault();
			return fault;
		}
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMNamespace ns = factory.createOMNamespace("", "");
		OMElement response = factory.createOMElement("Price", ns);
		response.setText(String.valueOf(i++));
		return response;
	}

	private SOAPFault getSOAPFault() {
		MessageContext context = MessageContext.getCurrentMessageContext();
		SOAPFactory factory = null;
		if (context.isSOAP11()) {
			factory = OMAbstractFactory.getSOAP11Factory();
		} else {
			factory = OMAbstractFactory.getSOAP12Factory();
		}
		SOAPFault fault = factory.createSOAPFault();
		SOAPFaultCode faultCode = factory.createSOAPFaultCode(fault);
		faultCode.setText("13");
		factory.createSOAPFaultValue(faultCode);
		SOAPFaultReason faultReason = factory.createSOAPFaultReason(fault);
		faultReason.setText("request can not be null");
		factory.createSOAPFaultText(faultReason);
		factory.createSOAPFaultDetail(fault);
		return fault;
	}
}